package com.yaozheng.vocationaltraining.fragment;

import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.activity.PassThroughModeMenuActivity;
import com.yaozheng.vocationaltraining.bean.BaoXianBean;
import com.yaozheng.vocationaltraining.bean.CarDetailBean;
import com.yaozheng.vocationaltraining.bean.DownPaymentBean;
import com.yaozheng.vocationaltraining.bean.RepaymentReriodBean;
import com.yaozheng.vocationaltraining.utils.LogUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by moon on 2017/11/21.
 */

@EFragment(R.layout.activity_allprice)
public class AllPriceFragment extends BaseFragment {

    @ViewById
    TextView all_price_textview;
    @ViewById
    TextView name_textview;
    @ViewById
    EditText price_textview;
    @ViewById
    TextView must_price_textview;
    @ViewById
    TextView shangyebaoxian_textview;


    @ViewById
    LinearLayout must_pay_layout;
    @ViewById
    LinearLayout shangyebaoxian_layout;

    public ArrayList<DownPaymentBean> getDownPaymentBeanList() {
        return downPaymentBeanList;
    }

    public void setDownPaymentBeanList(ArrayList<DownPaymentBean> downPaymentBeanList) {
        this.downPaymentBeanList = downPaymentBeanList;
    }

    public ArrayList<RepaymentReriodBean> getRePaymentPeriodBeanList() {
        return rePaymentPeriodBeanList;
    }

    public void setRePaymentPeriodBeanList(ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList) {
        this.rePaymentPeriodBeanList = rePaymentPeriodBeanList;
    }

    ArrayList<DownPaymentBean> downPaymentBeanList = new ArrayList<>();
    ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList = new ArrayList<>();

//    public CarDetailBean getCarDetailBean() {
//        return carDetailBean;
//    }
//
//    public void setCarDetailBean(CarDetailBean carDetailBean) {
//        this.carDetailBean = carDetailBean;
//        price = (int) (carDetailBean.getPrice() * 10000);
//    }
//
//    CarDetailBean carDetailBean;

    int price = 0;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    @AfterViews
    public void initView() {

        initUIData();


        price_textview
                .setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId,
                                                  KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE
                                || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                            PriceFragment.carDetailBean.setPrice(
                                (Float.parseFloat(price_textview.getEditableText().toString()) / 10000.0f)
                            );

                            getHomeActivity().refreshUI();
                            return true;
                            }
                            return false;
                        }
                });

//        price_textview.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                PriceFragment.carDetailBean.setPrice(
//                        (Float.parseFloat(price_textview.getEditableText().toString()) / 10000.0f)
//                );
//            }
//        });


    }

    public void initUIData() {
        price_textview.setText(Math.round(PriceFragment.carDetailBean.getPrice() * 10000)+"");
        name_textview.setText(PriceFragment.categoryName + " "+PriceFragment.carDetailBean.getName());

        int mustPrice = PriceFragment.carDetailBean.getGouzhishui()
                + PriceFragment.carDetailBean.getShangpaifei() +
                PriceFragment.carDetailBean.getChechuanshiyongshui() +
                PriceFragment.carDetailBean.getJiaoqiangxian() +
                PriceFragment.carDetailBean.getXiaofeishui();
        must_price_textview.setText(mustPrice+"");

//        第三者责任险：调用表Car里的disanzhezerenxian【数组】
//        车辆损失险：调用表Car里的cheliangshunshixian
//        全车盗抢险：调用表Car里的quanchedaoqiangxian
//        玻璃单独破碎险：调用表Car里的bolidanduposuixian【数组】
//        自燃损失险：调用表Car里的ziransunshixian
//        不计免赔特约险=(车辆损失险+第三者责任险)×20%
//        无过责任险=第三者责任险×20%
//        车上人员责任险：调用表Car里的cheshangrenyuanzerenxian
//        车身划痕险：调用表Car里的cheshenhuahenxian【数组】
//        涉水险：调用表Car里的sheshuixian


        int disanzhezerenxianPrice = 0 ;
        int cheliangsunshixianPrice = PriceFragment.carDetailBean.getCheliangshunshixian();
        int quanchedaoqiangxianPrice = PriceFragment.carDetailBean.getQuanchedaoqiangxian();
        int bolidanduposuixianPrice = 0;
        int ziransunshixianPrice = PriceFragment.carDetailBean.getZiransunshixian();
        int bujimianpeiteyuexianPrice = PriceFragment.carDetailBean.getBujimianpeiteyuexian();
        int wuguozerenxianPrice = PriceFragment.carDetailBean.getWuguozerenxian();
        int cheshangrenyuanzerenxianPrice = PriceFragment.carDetailBean.getCheshangrenyuanzerenxian();
        int cheshenghuahengxianPrice = 0;
        int sheshuixianPrice = PriceFragment.carDetailBean.getSheshuixian();

        for(BaoXianBean bean : PriceFragment.carDetailBean.getDisanzhezerenxian()){
            if(bean.getDefaultStatus().equals("1")){
                disanzhezerenxianPrice = Math.round( bean.getValue());
            }
        }
        for(BaoXianBean bean : PriceFragment.carDetailBean.getBolidanduposuixian()){
            if(bean.getDefaultStatus().equals("1")){
                bolidanduposuixianPrice = Math.round(PriceFragment.carDetailBean.getPrice()* 10000 *bean.getValue());
            }
        }
        for(BaoXianBean bean : PriceFragment.carDetailBean.getCheshenhuahenxian()){
            if(bean.getDefaultStatus().equals("1")){
                cheshenghuahengxianPrice = Math.round( bean.getValue());
            }
        }

        int shangyebaoxianPrice =
                (PriceFragment.carDetailBean.isHas_disanzhezerenxian()? disanzhezerenxianPrice:0) +
                        (PriceFragment.carDetailBean.isHas_cheliangshunshixian()?cheliangsunshixianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_quanchedaoqiangxian()?quanchedaoqiangxianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_bolidanduposuixian()?bolidanduposuixianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_ziransunshixian()?ziransunshixianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_bujimianpeiteyuexian()?bujimianpeiteyuexianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_wuguozerenxian()?wuguozerenxianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_cheshangrenyuanzerenxian()?cheshangrenyuanzerenxianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_cheshenhuahenxian()?cheshenghuahengxianPrice:0)+
                        (PriceFragment.carDetailBean.isHas_sheshuixian()?sheshuixianPrice:0) ;
        shangyebaoxian_textview.setText(shangyebaoxianPrice+"");


        all_price_textview.setText(Math.round(PriceFragment.carDetailBean.getPrice() * 10000 +
                mustPrice + shangyebaoxianPrice)+"");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.println("------AllPriceFragment onStart-------");
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.println("------AllPriceFragment onResume-------");
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.println("------AllPriceFragment onPause-------");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.println("------AllPriceFragment onStop-------");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.println("------AllPriceFragment onDestroyView-------");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.println("------AllPriceFragment onDestroy-------");
    }

    @Click({R.id.must_pay_layout})
    public void mustPayLayoutClick(View view) {
        getHomeActivity().openMustPayFragment(PriceFragment.carDetailBean);
    }
    @Click({R.id.shangyebaoxian_layout})
    public void shangYeBaoXianLayoutClick(View view) {
        getHomeActivity().openShangYeBaoXianFragment(PriceFragment.carDetailBean);
    }



}
